import { request } from "../request";
import { getAppClientInfo } from "../../config/app";

/**
 * 聊天室相关 API
 */
export const chatApi = {
  // 获取聊天节点
  getChatNode: () => {
    return request("/chat-room/node/get", "GET");
  },
  /**
   * 获取聊天室消息列表
   * @param {number} page - 页码
   * @returns {Promise} 返回消息列表
   */
  getMessages: (page) => {
    return request("/chat-room/more", "GET", { page: page || 1 });
  },
  //获取默认表情
  getDefaultEmoticon: () => {
    return request("/users/emotions", "GET");
  },
  // 获取表情包
  getEmoticon: (gameId = "emojis") => {
    return request("/api/cloud/get", "POST", { gameId });
  },
  //同步表情包 同步指定 Key 数据到云，这里用于同步用户表情包
  syncEmoticon: (gameId = "emojis", data) => {
    return request("/api/cloud/sync", "POST", { gameId, data });
  },
  //获取禁言中成员列表（思过崖）
  getSilencedMembers: () => {
    return request("/chat-room/si-guo-list", "GET");
  },
  // 撤回消息
  withdrawMessage: (oId) => {
    return request(`/chat-room/revoke/${oId}`, "DELETE");
  },
  /**
   * 发送聊天消息
   * @param {string} content - 消息内容
   * @returns {Promise} 返回发送结果
   */
  sendMessage: (content, client = getAppClientInfo()) => {
    return request("/chat-room/send", "POST", { content, client });
  },

  /**
   * 获取私信列表
   * @returns {Promise} 返回私信列表
   */
  getMessageList: () => {
    return request("/chat/get-list", "GET");
  },

  /**
   * 发送私信
   * @param {string} username - 接收者用户名
   * @param {string} content - 消息内容
   * @returns {Promise} 返回发送结果
   */
  sendPrivateMessage: (username, content) => {
    return request("/chat/send", "POST", { username, content });
  },
  //打开红包
  getRedPacket: (oId, gesture) => {
    return request(`/chat-room/red-packet/open`, "POST", { oId, gesture });
  },
  /**
   * 获取私信详情
   * @param {string} username - 对方用户名
   * @param {number} page - 页码
   * @param {number} pageSize - 每页数量
   * @returns {Promise} 返回私信详情列表
   */
  getMessageDetail: (toUser, page = 1, pageSize = 20) => {
    return request("/chat/get-message", "GET", { toUser, page, pageSize });
  },
  //获取未读消息
  getUnreadMessage: () => {
    return request("/chat/has-unread", "GET");
  },
  //标记用户消息已读
  readMessage: (fromUser) => {
    return request("/chat/mark-as-read", "GET", { fromUser });
  },
  // 发送红包
  sendRedPacket: (redPacketData) => {
    // 直接使用已经生成好格式的消息发送
    return request("/chat-room/send", "POST", {
      content: redPacketData,
      client: getAppClientInfo(),
    });
  },
};
